package com.jxmcloud.business.cut.service.impl.provider;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.dtoCommon.AccountInfoDto;
import com.jxmcloud.business.cut.model.database.DBUserActivityInfo;
import com.jxmcloud.business.cut.model.database.DBUserLoginInfo;
import com.jxmcloud.business.cut.props.CutProperties;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.database.IUserInviteSuccessDBService;
import com.jxmcloud.business.cut.service.database.IUserLoginInfoDBService;
import com.jxmcloud.business.cut.service.provider.IUserCutInfoService;
import com.jxmcloud.business.cut.util.VipRpcUtil;
import com.xlcloud.platform.util.DateUtil;
import com.xlcloud.platform.util.PublicUtil;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class IUserCutInfoServiceImpl implements IUserCutInfoService {
    @Resource
    private IUserLoginInfoDBService userService;
    @Resource
    private VipRpcUtil vipRpcUtil;
    @Resource
    private IUserActivityInfoDBService userActivityInfoDBService;
    @Resource
    private IUserInviteSuccessDBService userInviteSuccessDBService;
    @Resource
    private CutProperties cutProperties;
    /**
     * 查询用户在砍一刀参加的活动信息
     * @param accountInfoDto
     * @return
     */
    public String getCutInfoBuyUser(AccountInfoDto accountInfoDto){
        //查询是否参加过砍一刀的活动
        DBUserLoginInfo dbUserLoginInfo = userService.selectByAccount(accountInfoDto.getAccount());
        //没有来过砍一刀
        if(ObjectUtil.isEmpty(dbUserLoginInfo)){
            //查看购买商品是否超过 通知A配置件数
            if(vipRpcUtil.getUserTotalBuyNum(accountInfoDto.getAccount(), null)
                    >= cutProperties.getNoticeA().getNum()){
                return cutProperties.getNoticeA().getNotice();
            }
        }else {
            //---------------------------来过砍一刀，或者 助力----------------------------------
            //参加过砍一刀超过3个月, 看最后参加活动时间
            List<DBUserActivityInfo> dbUserActivityInfos = userActivityInfoDBService.selectAllByUid(dbUserLoginInfo.getUid());
            if(ObjectUtil.isNotEmpty(dbUserActivityInfos)){
                DBUserActivityInfo first = PublicUtil.first(dbUserActivityInfos);
                //判断是否参加活动超三月
                Date date = DateUtil.addMonth(first.getCreateTime(), 3);
                //时间超三个月
                if(DateUtil.isAfterStart(DateUtil.dateToChinese2(date),DateUtil.newDateYMDHms())){
                    //帮别人助力过
                    if(userInviteSuccessDBService.checkExitByInvitedAccount(accountInfoDto.getAccount())){
                        return cutProperties.getNoticeC().getNotice();
                    }
                    //查看购买商品是否超过 通知B配置件数
                    if(vipRpcUtil.getUserTotalBuyNum(accountInfoDto.getAccount(), null)
                            >= cutProperties.getNoticeB().getNum()){
                        return cutProperties.getNoticeB().getNotice();
                    }
                }
            }
        }
        return null;
    }

}
